package com.frxuexi.document.view

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.document.DocIntent
import com.frxuexi.document.DocViewModel
import com.frxuexi.document.entity.DocEntity
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import kotlinx.coroutines.launch

@Composable
fun DocPage(navHostController: NavHostController, viewModel: DocViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<DocEntity>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            viewModel.state.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        if(it.type==RequestType.DELETE){
                            ToastUtils.showLong("删除成功")
                            list.clear()
                            viewModel.sendIntent(
                                DocIntent.getDocs(
                                    mapOf(
                                        Pair("userId", 1),
                                        Pair("page", 1)
                                    )
                                )
                            )
                        }else {

                            Log.i("===", it.data.toString())
                            list.addAll(it.data as Collection<DocEntity>)
                        }
                    }

                    else -> {}
                }
            }
        }
        viewModel.sendIntent(DocIntent.getDocs(mapOf(Pair("userId", 1), Pair("page", 1))))
    })

    Scaffold(floatingActionButton = {
        Icon(painter = painterResource(id = R.drawable.icon159), contentDescription = "",
            modifier = Modifier.clickable {
                navHostController.navigate(RoutePath.LOCALDOC)
            })
    }) {
        Column(
            Modifier
                .padding(it)
                .fillMaxSize()
                .padding(20.dp, 20.dp, 20.dp, 0.dp)
        ) {
            BaseTitle(navHostController = navHostController, title = "文件")
            Spacer(modifier = Modifier.height(10.dp))
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list) {
                    DocItem(entity = it,viewModel){
//                    list-=it
                    }
                }
            })
        }
    }

}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DocItem(entity: DocEntity, viewModel: DocViewModel, deleteClick:() -> Unit) {
    Row(
        modifier = Modifier.combinedClickable(
            onLongClick = {
                viewModel.sendIntent(DocIntent.deleteDoc(mapOf("FileId" to entity.fileId)))
                deleteClick()
            },
            onDoubleClick = {
                ToastUtils.showLong("双击了")
            },
            onClick = {
                ToastUtils.showLong("点击了")
            })
    ) {
        AsyncImage(
            model = entity.filePath, contentDescription = "",
            modifier = Modifier
                .size(60.dp)
                .clip(RoundedCornerShape(20.dp)), contentScale = ContentScale.Crop
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceAround) {
            Text(text = entity.fileName)
            val time = viewModel.toDateTime(entity.fileTime)
            Text(text = time)
        }
    }
}